/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.engagemangsindex.ws;

import se.sll.engagemangsindex.exceptions.DuplicateUpdateException;
import se.sll.engagemangsindex.exceptions.InvalidRoutingException;
import java.util.List;
import javax.jws.WebService;
import javax.xml.soap.SOAPException;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.annotations.SchemaValidation;
import riv.itintegration.engagementindex._1.ResultCodeEnum;
import riv.itintegration.engagementindex.processnotification._1.rivtabp21.ProcessNotificationResponderInterface;
import riv.itintegration.engagementindex.processnotificationresponder._1.ProcessNotificationResponseType;
import riv.itintegration.engagementindex.processnotificationresponder._1.ProcessNotificationType;
import se.sll.engagemangsindex.dto.EngagementTransactionTypeImpl;
import se.sll.engagemangsindex.dto.EngagementTypeImpl;
import se.sll.engagemangsindex.entity.Engagement;

/**
 *
 * @author khaleddaham
 */
@WebService(endpointInterface = "riv.itintegration.engagementindex.processnotification._1.rivtabp21.ProcessNotificationResponderInterface",
        serviceName = "ProcessNotification")
@SchemaValidation
@Slf4j
public class ProcessNotificationWSImpl extends BaseUpdateImpl implements ProcessNotificationResponderInterface {

    /**
     *
     */
    public ProcessNotificationWSImpl() {
        super();
    }

    /**
     *
     * @param logicalAddress
     * @param parameters
     * @return
     * @throws SOAPException
     */
    @Override
    public ProcessNotificationResponseType processNotification(String logicalAddress, ProcessNotificationType parameters) {
        ProcessNotificationResponseType response = new ProcessNotificationResponseType();
        log.debug("[processNotification]");
        List<EngagementTransactionTypeImpl> engagementTransactionList = this.copy(parameters.getEngagementTransaction());
        response.setComment(null);
        response.setResultCode(ResultCodeEnum.OK);
        try {
            this.updateAllEngagements(logicalAddress, engagementTransactionList);
        } catch (InvalidRoutingException ex) {
            log.info(ex.getMessage(), ex);
            response.setComment(ex.getMessage());
            response.setResultCode(ResultCodeEnum.ERROR);
        } catch (DuplicateUpdateException ex) {
            log.info(ex.getMessage(), ex);
            response.setComment(ex.getMessage());
            response.setResultCode(ResultCodeEnum.ERROR);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            this.throwSoapFaultException(ex.getMessage());
        }
        return response;
    }

    /**
     *
     * @param logicalAddress
     * @param engagementTransactionList
     * @throws InvalidRoutingException
     * @throws DuplicateUpdateException
     */
    protected void updateAllEngagements(String logicalAddress, List<EngagementTransactionTypeImpl> engagementTransactionList) throws InvalidRoutingException, DuplicateUpdateException {
        this.checkForMatchingOwner(logicalAddress);
        this.checkForDuplicateUpdates(engagementTransactionList);
        for (EngagementTransactionTypeImpl engagement : engagementTransactionList) {
            EngagementTypeImpl e = engagement.getEngagement();
            Engagement checkEngagement = EngagementHelper.toEngagement(e);
            if (this.hasExistingEngagementWithDifferentOwner(checkEngagement)) {
                log.warn("Owner mismatch.");
            }
            if (engagement.isDeleteFlag()) {
                this.removeEngagement(e);
            } else {
                this.addEngagement(e);
            }
        }
    }
}